﻿/* -----------------------------------------------------------------------
 * <copyright file="OPScoreDetailQuery.cs" company="O-RID">
 *     Copyright (c)2010 O-RID Co.,Ltd.
 * </copyright>
 * <author>Li Dawei</author>
 * <summary> 
 *          OP别 详细成绩查询
 *          作成日：06/13/2010
 *          
 *           08/19/2010 修改 前台OPID框。
 *                          当选择的是 所有OP或组时。用TAG值作为判断条件，用text值作为显示值
 *                          选择部分OP时，方法不变
 *                          
 *           07/06/2011 修改 账票种类查询方式。v2.15
 *                     修改 文字种类查询方式。v2.15
 *                     修改 JobClass种类查询方式。v2.15
 *                     增加 案件名，线路查询条件
 *                     
 * </summary> 
 * -----------------------------------------------------------------------
 */

namespace Orid.Management.Statistics.OperatorUnitMangement.Command.OP
{
    using System;
    using System.Text;
    using System.Windows;
    using System.Windows.Media;
    using Orid.Management.Statistics.OperatorUnitMangement.Common.FunctionList;
    using Orid.Management.Statistics.OperatorUnitMangement.Common.SearchCtrl;
    using Orid.Management.Statistics.OperatorUnitMangement.WCFServers.OPQueryRef;
    using childWindow = Orid.Management.Statistics.OperatorUnitMangement.Common.Window;
    using Wcf = Orid.Management.Statistics.OperatorUnitMangement.Common.Service;

    /// <summary>
    ///  OP别 详细成绩查询命令实现接口ICommand
    /// </summary>
    public class OPScoreDetailQuery : ICommand
    {
        #region 变量定义

        /// <summary>
        /// 查询区域对象 Container
        /// </summary>
        private SelAllContainer container;

        /// <summary>
        /// 错误消息提示 ErrorMessagebox
        /// </summary>
        private childWindow.MessageBox errorMessbox ;

        /// <summary>
        /// 查询服务定义 wcfOPScoreDetailClient
        /// </summary>
        private WCFServers.OPQueryRef.WCFOPCorrelationClient wcfOPScoreDetailClient;

        /// <summary>
        /// 控件Tab值 wcfOPPEntryClient
        /// </summary>
        //private WCFServers.OPQueryRef.WCFOPCorrelationClient wcfOPPEntryClient;

        /// <summary>
        /// 日志服务定义 wcfLogClient
        /// </summary>
        private WCFLog4Ref.WCFLog4Client wcfLogClient;

        #endregion  变量定义

        /// <summary>
        ///  OP别 详细成绩查询 OPScoreDetailQuery
        /// </summary>
        /// <param name="container"> 查询区域对象 container</param>
        public OPScoreDetailQuery(SelAllContainer container)
        {
            this.container = container;

            this.InitField();
        }

        /// <summary>
        /// 查询完成事件 ExecOver
        /// </summary>
        public event EventHandler<SelectOPScoreDetailCompletedEventArgs> ExecOver;

        /// <summary>
        /// 查询开始事件 ExecBegin
        /// </summary>
        public event EventHandler<EventArgs> ExecBegin;
               
        #region ICommand 成员

        /// <summary>
        /// 详细成绩查询命令的具体实现 实现接口ICommand
        /// </summary>
        public void Exec()
        {
            // this.tbOPID.Text dpDateFrom dpDateTo内容必选的判断
            // 日期不能相同
            // 开始日期不能小于结束日期
            int error = FunctionClass.IsSelectDo(
                                        this.container.dpDateFrom, 
                                        this.container.dpDateTo, 
                                        this.container.tbOPID,
                                        this.errorMessbox);

            if (error == 0)
            {
                this.container.btnSelectAllInfor.IsEnabled = true;
                return;
            }

            // 把WHERE值组合好后放入到CWP中
            WCFServers.OPQueryRef.WhereParameters cwp = new WhereParameters();

            try
            {
                try
                {
                    #region 把参数加到cwp中
                    // 日期的判断条件
                    // 日期返回空值用"null"
                    if (!String.IsNullOrEmpty(this.container.dpDateFrom.Text))
                    {
                        cwp.WhereentryDateFrom = this.container.dpDateFrom.SelectedDate.Value.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        cwp.WhereentryDateFrom = "null";
                    }

                    if (!String.IsNullOrEmpty(this.container.dpDateTo.Text))
                    {
                        cwp.WhereentryDateTo = this.container.dpDateTo.SelectedDate.Value.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        cwp.WhereentryDateTo = "null";
                    }

                    // 帐票种类选择条件 07/05/2011 v2.15
                    if (ClientDataList.SelScriptTypeList.Count > 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        foreach (string s in ClientDataList.SelScriptTypeList)
                        {
                            // 添加逗号
                            sb.Append(s + ",");
                        }

                        // 去除尾部多余逗号
                        cwp.WhereScriptType = sb.ToString().Remove(sb.ToString().Length - 1, 1);
                    }
                    // job种类 选择条件 07/06/2011 v2.15
                    if (ClientDataList.SelJobClassList.Count > 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        foreach (string s in ClientDataList.SelJobClassList)
                        {
                            // 添加逗号
                            sb.Append(s + ",");
                        }

                        // 去除尾部多余逗号
                        cwp.WhereJobClass = sb.ToString().Remove(sb.ToString().Length - 1, 1);
                    }

                    // 文字种类选择条件 07/06/2011 v2.15
                    if (ClientDataList.SelWordTypeList.Count > 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        foreach (string s in ClientDataList.SelWordTypeList)
                        {
                            // 添加逗号
                            sb.Append(s + ",");
                        }

                        // 去除尾部多余逗号
                        cwp.WhereWordType = sb.ToString().Remove(sb.ToString().Length - 1, 1);
                    }
                    // 判断是ALL还是部分OPID
                    if (this.container.tbOPID.Tag.ToString().Contains("ALL"))
                    {
                        // 选择一组或几组OP
                        StringBuilder sb = new StringBuilder();
                        if (ClientDataList.CheckOpUnitList.Count > 0)
                        {
                            foreach (string s in ClientDataList.CheckOpUnitList)
                            {
                                // 添加逗号
                                sb.Append(s + ",");
                            }

                            // 去除尾部多余逗号
                            cwp.WhereUnit = sb.ToString().Remove(sb.ToString().Length - 1, 1);
                        }

                        cwp.WhereLoginName = null;
                    }
                    else
                    {
                        // 选择部分OP
                        // 已加完逗号，不用再转换
                        cwp.WhereLoginName = FunctionClass.MyIsNullOrEmpty(this.container.tbOPID.Text.Trim().ToString(), null);
                        cwp.WhereUnit = null;
                    }

                    // LINE选择条件 07/06/2011
                    if (ClientDataList.SelLineList.Count > 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        foreach (string s in ClientDataList.SelLineList)
                        {
                            // 添加逗号
                            sb.Append(s + ",");
                        }

                        // 去除尾部多余逗号
                        cwp.WhereServer = sb.ToString().Remove(sb.ToString().Length - 1, 1);
                    }


                    // 案件名选择条件 07/06/2011
                    if (ClientDataList.SelMatterNamesList.Count > 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        foreach (string s in ClientDataList.SelMatterNamesList)
                        {
                            // 添加逗号
                            sb.Append(s + ",");
                        }

                        // 去除尾部多余逗号
                        cwp.WhereMatterName = sb.ToString().Remove(sb.ToString().Length - 1, 1);
                    }


                    #endregion 把参数加到cwp中
                }
                catch (Exception ex)
                {
                    this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + "  OP别详细成绩查询接口实现类 OPEntrySpeedQueryControl 转换查询条件出错" + ex.Message.ToString());

                    Common.FunctionList.FunctionClass.ShowMessageBox(
                       this.errorMessbox,
                       1,
                       "转换查询条件出错",
                       ex.Message.ToString(),
                       Colors.Gray,
                       true,
                       Visibility.Visible,
                       Visibility.Collapsed);
                    return;
                }

                this.OnExecBegin(null, null);

            
                this.wcfOPScoreDetailClient.SelectOPScoreDetailAsync(cwp);
            }
            catch (Exception ex)
            {
                this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + 
                    "OP别详细成绩查询接口实现类 OPEntrySpeedQueryControl 调用SelectOPEntrySpeedQueryInfor服务出错" + 
                    ex.Message.ToString());
     
                Common.FunctionList.FunctionClass.ShowMessageBox(
                   this.errorMessbox,
                   1,
                   "调用SelectOPScoreDetailQueryInfor服务出错",
                   ex.Message.ToString(),
                   Colors.Gray,
                   true,
                   Visibility.Visible,
                   Visibility.Collapsed);
                return;
            }             
        } 

        #endregion

        /// <summary>
        /// 查询结束事件 OnExecOver
        /// </summary>
        /// <param name="sender">Sender of Object</param>
        /// <param name="e">e type of SelectOPScoreDetailCompletedEventArgs</param>
        protected void OnExecover(object sender, SelectOPScoreDetailCompletedEventArgs e)
        {
            if (this.ExecOver != null)
            {
                this.ExecOver(this, e);
            }
        }

        /// <summary>
        /// 查询开始事件 OnExecBegin
        /// </summary>
        /// <param name="sender">Sender of Object</param>
        /// <param name="e">e type of EventArgs</param>
        protected void OnExecBegin(object sender, EventArgs e)
        {
            if (this.ExecBegin != null)
            {
                this.ExecBegin(this, e);
            }
        }

        /// <summary>
        /// 初始化变量 InitField
        /// </summary>  
        private void InitField()
        {
            this.wcfOPScoreDetailClient = Wcf.Service.OPScoreDetailQuery;

            this.errorMessbox = new childWindow.MessageBox();

            this.wcfLogClient = Wcf.Service.Log4;

            this.wcfOPScoreDetailClient.SelectOPScoreDetailCompleted += new EventHandler<SelectOPScoreDetailCompletedEventArgs>(this.OnExecover);

        }
    }
}
